using Microsoft.SqlServer.Management.Smo;
using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Text;

namespace Microsoft.SqlServer.Management.SqlManagerUI.Scripting
{
using Server = Microsoft.SqlServer.Management.Smo.Server;
	internal class ScriptFullTextCatalogCommand : ScriptObjectCommand
	{
		private const string _ifExistsCheck = "IF EXISTS (SELECT * FROM sysfulltextcatalogs ftc WHERE ftc.name = N'{0}')";

		private const string _dropFrom2005 = "DROP FULLTEXT CATALOG [{0}]";

		private const string _dropFrom2000 = "EXEC dbo.sp_fulltext_catalog @ftcat=N'{0}', @action=N'drop'";

		public ScriptFullTextCatalogCommand(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptAction) : base(server, scriptTarget, scriptAction)
		{
		}

		protected override void ScriptDrop(SqlSmoObject smoObject, ScriptingOptions options, ObjectScriptedHandler scriptTarget)
		{
			FullTextCatalog fullTextCatalog = (FullTextCatalog)smoObject;
			StringBuilder stringBuilder = new StringBuilder();
			if (options.IncludeIfNotExists)
			{
				stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "IF EXISTS (SELECT * FROM sysfulltextcatalogs ftc WHERE ftc.name = N'{0}')", new object[]
				{
					ReflectionUtilities.EscapeString(fullTextCatalog.Name, '\'')
				});
				stringBuilder.AppendLine();
			}
			if (options.TargetServerVersion == SqlServerVersion.Version90)
			{
				stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "DROP FULLTEXT CATALOG [{0}]", new object[]
				{
					ReflectionUtilities.EscapeString(fullTextCatalog.Name, ']')
				});
			}
			else
			{
				stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "EXEC dbo.sp_fulltext_catalog @ftcat=N'{0}', @action=N'drop'", new object[]
				{
					ReflectionUtilities.EscapeString(fullTextCatalog.Name, '\'')
				});
			}
			StringCollection stringCollection = new StringCollection();
			stringCollection.Add(stringBuilder.ToString());
			scriptTarget(new ObjectInfo(fullTextCatalog.Name, fullTextCatalog.Urn), stringCollection, ScriptActions.Drop);
		}

		protected override void ScriptPermission(SqlSmoObject smoObject, ScriptingOptions options, ObjectScriptedHandler scriptTarget)
		{
			FullTextCatalog fullTextCatalog = (FullTextCatalog)smoObject;
			if (fullTextCatalog.Parent.Parent.Information.Version.MajorRevision >= 9 && options.TargetServerVersion == SqlServerVersion.Version90)
			{
				base.ScriptPermission(smoObject, options, scriptTarget);
			}
		}

		protected override void ScriptCreate(SqlSmoObject smoObject, ScriptingOptions options, ObjectScriptedHandler scriptTarget)
		{
			options.FullTextIndexes = false;
			options.IncludeDatabaseContext = false;
			FullTextCatalog fullTextCatalog = (FullTextCatalog)smoObject;
			DateTime arg_1B_0 = fullTextCatalog.PopulationCompletionDate;
			fullTextCatalog.RootPath = string.Empty;
			if (fullTextCatalog.Parent.Parent.Information.Version.MajorRevision >= 9)
			{
				fullTextCatalog.FileGroup = string.Empty;
			}
			base.ScriptCreate(smoObject, options, scriptTarget);
		}
	}
}
